<template>
  <div>
    <a-card class="card-header">
      <a-form-model :model="queryParam">
        <a-row :gutter="24">
          <a-col :span="5">
            <a-form-model-item
              label="计划单号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.planNum"
                placeholder="请输入计划单号"
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="商品编号"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-input
                v-model.trim="queryParam.goodsCode"
                placeholder="请输入商品编号："
              ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="5">
            <a-form-model-item
              label="计划日期"
              :labelCol="labelCol"
              :wrapperCol="wrapperCol"
            >
              <a-range-picker
                style="width:100%"
                :placeholder="['开始时间', '结束时间']"
                @change="onRangeChange"
              />
            </a-form-model-item>
          </a-col>
          <a-col :span="2">
            <a-button
              type="primary"
              style="margin-top:3px"
              @click="handleSearch"
              >查询</a-button
            >
          </a-col>
        </a-row>
      </a-form-model>
    </a-card>

    <!-- 表格区域 -->
    <a-card class="card-content">
      <div class="contetnBox">
        <a-row :gutter="24" class="headerBox">
          <a-col :span="22">
            <a-button type="primary" @click="handleAdd">添加生产计划</a-button>
          </a-col>
          <a-col :span="2">
            <a-button type="primary" @click="handleAllList('ProductPlanList')"
              >全部生产计划</a-button
            >
          </a-col>
        </a-row>
        <br />
        <a-table
          :columns="columns"
          :data-source="tableData"
          :loading="loading"
          :pagination="pagination"
          @change="handleTableChange"
          :rowKey="(record, index) => record.id"
          bordered
        >
          <template slot="status" slot-scope="text, record">
            <a-popconfirm
              okText="确定"
              cancelText="取消"
              v-if="tableData.length"
              title="确定修改状态吗?"
              @confirm="() => handleStatus(record)"
            >
              <a-switch
                :checked="record.status == 0 ? false : true"
                checked-children="已完成"
                un-checked-children="未完成"
                default-checked
              />
            </a-popconfirm>
          </template>

          <template slot="action" slot-scope="text, record">
            <a-badge :count="record.sonCount" :offset="offset">
              <a @click="handleToDetail('/production/plan/detail', record)"
                >下级子计划</a
              >
            </a-badge>
            <a-divider type="vertical" style="margin-left: 25px" />
            <a @click.stop="handleWork(record)">工序</a>
            <a-divider type="vertical" />
            <a @click.stop="handleEdit(record)">编辑</a>
            <a-divider type="vertical" />
            <a @click.stop="handleWorkOrder(record)">详情</a>
            <a-divider
              type="vertical"
              v-if="record.automaticScheduling === 0"
            />
            <a
              @click.stop="handleWorkAutoMatic(record)"
              v-if="record.automaticScheduling === 0"
              >自动排单</a
            >
            <a-divider type="vertical" />
            <a-icon
              type="qrcode"
              @click.stop="handleCode(record)"
              style="fontSize: 16px;color: #40a9ff;"
            />
            <!-- <a-divider type="vertical" />
            <span @click.stop>
              <a-popconfirm
               okText="确定"
            cancelText="取消"
                v-if="tableData.length"
                title="确定删除吗?"
                @confirm="() => handleDelete(record)"
              >
                <a>删除</a>
              </a-popconfirm>
            </span> -->
          </template>
        </a-table>
        <PlanModal
          ref="modalForm"
          :disabled="disabled"
          @ok="modalFormOk"
        ></PlanModal>
      </div>
      <Procedure ref="Procedure" />

      <WorkOrderModal ref="WorkOrder" />

      <AutoMaticModal @ok="modalFormOk" ref="AutoMatic" />

      <!-- 二维码 -->
      <qrCode
        ref="qrCode"
        :label="qrModel.label"
        :text="qrModel.text"
        :qrSize="320"
      ></qrCode>
    </a-card>
  </div>
</template>

<script>
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import { planColumns } from "./components/tableConfig";
import Procedure from "./components/Procedure";
import WorkOrderModal from "./components/WorkOrderModal.vue";
import AutoMaticModal from "./components/AutoMaticModal.vue";
import PlanModal from "@/components/PlanModal";
import qrCode from "@/components/qrCode";
import api from "@/api/apiServer";
import { CodeNo } from "@/unit/QRCodeConfig";

export default {
  mixins: [tableMinxins],
  components: { Procedure, PlanModal, WorkOrderModal, AutoMaticModal, qrCode },
  data() {
    return {
      columns: planColumns,
      offset: [8, -4],
      disabled: false,
      qrModel: {
        label: "", // 二维码底部内容
        text: "", // 二维码内容
      },
    };
  },
  activated() {
    this.getList();
  },
  mounted() {
    this.getList();
  },
  methods: {
    handleCode(record) {
      let { planNum, id, goodsId } = record;
      let newObj = { id, planNum, goodsId, QRCode: CodeNo.ProductionPlan };
      let text = JSON.stringify(newObj);
      this.$refs.qrCode.title = "生产计划二维码";
      this.qrModel.label = `${planNum} `;
      this.qrModel.text = text;
      this.$refs.qrCode.list = this.qrModel.label.split(",");
      this.$refs.qrCode.showModal();
    },
    // 获取列表数据
    async getList(arg) {
      this.loading = true;
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.pagination.current = 1;
      }
      var params = this.getQueryParams();
      const res = await api.ProductPlanList(params);
      this.tableData = res.data.data;
      this.pagination.total = res.data.total;
      this.loading = false;
    },
    // 工序详情
    handleWork(record) {
      this.$refs.Procedure.showDrawer(record.bomId);
    },
    // 工单详情
    handleWorkOrder(record) {
      this.$refs.WorkOrder.showDrawer(record.id);
    },
    // 自动生成加工单
    handleWorkAutoMatic(record) {
      let { id, planNum, planQuantity, bomId, goodsId } = record;
      this.$refs.AutoMatic.title = "自动生成加工单";
      this.$refs.AutoMatic.modalEdit({
        id,
        planNum,
        planQuantity,
        bomId,
        goodsId,
      });
    },
    async handleStatus(record) {
      let params = Object.assign(record, {});
      if (record.status === 0) {
        params.status = 1;
      } else {
        params.status = 0;
      }
      const res = await api.ProductPlanUpdate(params);
      if (res.code && res.code !== 200) {
        this.$message.error("修改失败");
        this.confirmLoading = false;
        return;
      }
      this.$message.success("修改成功");
      this.getList();
    },
    // 新增
    handleAdd() {
      this.disabled = false;
      this.$refs.modalForm.title = "新增生产计划";
      this.$refs.modalForm.modalAdd();
    },
    //  编辑
    handleEdit(record) {
      this.disabled = true;
      this.$refs.modalForm.title = "编辑生产计划";
      this.$refs.modalForm.modalEdit(record);
      this.getOption();
    },
    // 获取所有设备数据
    async getOption() {
      let params = { pageNumber: 1, pageSize: 999999, deleteFlag: "N" };
      const res = await api.CommodityList(params);
      this.$refs.modalForm.tableOption = res.data.data;
    },
    handleToDetail(path, record) {
      let { id } = record;
      let query = { id };
      this.$router.push({ path, query });
    },
    onRangeChange(dates, dateStrings) {
      this.$set(this.queryParam, "startTime", dateStrings[0]);
      this.$set(this.queryParam, "endTime", dateStrings[1]);
    },
    async handleDelete(record) {
      let params = {
        id: record.id,
      };
      const res = await api.ProductPlanDelete(params);
      this.$message.success(res.message);
      this.getList();
    },
    // 时间选择器
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
  button {
    margin-right: 13px;
  }
}
.card-content {
  .contetnBox {
    min-height: 809px;
    border: 1px solid #ebedf0;
    border-radius: 2px;
    padding: 20px 0;
    .headerBox {
      padding: 0 20px;
    }
  }
}
::v-deep .ant-form-item {
  margin-bottom: 0;
}
</style>
